var app = getApp();
var api = require('../../api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    management_good: false,
    selectAllStatus: false,  //底部全选
    cart:[],
    check_ids:[],
    total_price:0,
    rec_goods:[],
  },

  // 管理商品
  management: function () {
    let that = this;
    that.setData({
      management_good: true,
    })
  },
  finish_management: function () {
    let that = this;
    that.setData({
      management_good: false,
    })
  },

  gotoMall: function (e) {
    wx.switchTab({
      url: '/pages/mall/mall',
    })
  },

  
  /*** 生命周期函数--监听页面加载*/
  onLoad: function (options) {
    this.getRecgoods()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getMycart()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var page = this
    var user = wx.getStorageSync('user_info');
    return {
      path: '/pages/index/index?pid=' + user.user_id,
    }
  },
  //我的购物车
  getMycart:function(){
    var page = this
    wx.showLoading({
      title: '加载中',
    })
    app.request({
      url: api.shop.mycart,
      method: 'POST',
      data: {
      },
      success: function (res) {
        if (res.errcode == 0) {
          page.setData({
            cart: res.data,
          });
        }
      },
      fail: function (res) {
        wx.showModal({
          title: '警告',
          content: res.errmsg,
          showCancel: false
        })
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  //修改数量
  delNum: function (e) {

    var page = this;
    if (page.data.cart[e.currentTarget.dataset.key].num == 1) {
      wx.showToast({
        title: '数量最低为1',
        icon: 'none'
      })
      return false;
    } else {
      var cart = page.data.cart;
      cart[e.currentTarget.dataset.key].num = page.data.cart[e.currentTarget.dataset.key].num - 1;
      // wx.showLoading({
      //   title: '提交中',
      // })
      app.request({
        url: api.shop.update_cart,
        method: 'POST',
        data: {
          id: page.data.cart[e.currentTarget.dataset.key].cart_id,
          num: cart[e.currentTarget.dataset.key].num
        },
        success: function (res) {
          if (res.errcode == 0) {
            page.setData({
              cart: cart
            })
            page.reflushSelect();
          }
        },
        fail: function (res) {
          wx.showModal({
            title: '警告',
            content: res.errmsg,
            showCancel: false
          })
        },
        complete: function () {
          wx.hideLoading();
        }
      });

    }
  },

  addNum: function (e) {
    var page = this;
    var cart = page.data.cart;
    cart[e.currentTarget.dataset.key].num = page.data.cart[e.currentTarget.dataset.key].num + 1;
    // wx.showLoading({
    //   title: '提交中',
    // })
    app.request({
      url: api.shop.update_cart,
      method: 'POST',
      data: {
        id: page.data.cart[e.currentTarget.dataset.key].cart_id,
        num: cart[e.currentTarget.dataset.key].num
      },
      success: function (res) {
        if (res.errcode == 0) {
          page.setData({
            cart: cart
          })
          page.reflushSelect();
        }
      },
      fail: function (res) {
        wx.showModal({
          title: '警告',
          content: res.errmsg,
          showCancel: false
        })
      },
      complete: function () {
        wx.hideLoading();
      }
    });

    
    

  },
  //更新
  updateCart:function(){
    var page = this
    wx.showLoading({
      title: '加载中',
    })
    app.request({
      url: api.shop.mycart,
      method: 'POST',
      data: {
      },
      success: function (res) {
        if (res.errcode == 0) {
          page.setData({
            cart: res.data,
          });
        }
      },
      fail: function (res) {
        wx.showModal({
          title: '警告',
          content: res.errmsg,
          showCancel: false
        })
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  //删除购物车
  delCart:function(){
    var page = this;
    if (page.data.check_ids.length<=0){
      wx.showToast({
        title: '请选择要删除的商品',
        icon:'none'
      })
      return false;
    }else{
      wx.showLoading({
        title: '提交中',
      })
      app.request({
        url: api.shop.delcart,
        method: 'POST',
        data: {
          ids: page.data.check_ids
        },
        success: function (res) {
          if (res.errcode == 0) {
            page.onShow();
            page.setData({
              management_good:false,
              selectAllStatus:false,
              check_ids:[],
            })
          }
        },
        fail: function (res) {
          wx.showModal({
            title: '警告',
            content: res.errmsg,
            showCancel: false
          })
        },
        complete: function () {
          wx.hideLoading();
        }
      });
    }
  },
  //选择
  checkchange:function(e){
    console.log(e);
    var page =this;
    var check_ids = page.data.check_ids;
    var money = page.data.total_price;
    //未选择
    if (check_ids.indexOf(e.currentTarget.dataset.id)==-1){
      money = money + (page.data.cart[e.currentTarget.dataset.index_key].price * page.data.cart[e.currentTarget.dataset.index_key].num);
      check_ids.push(e.currentTarget.dataset.id);
    }else{
      //已选择
      check_ids.splice(check_ids.indexOf(e.currentTarget.dataset.id),1);
      money = money - (page.data.cart[e.currentTarget.dataset.index_key].price * page.data.cart[e.currentTarget.dataset.index_key].num);
    }
    page.setData({
      check_ids: check_ids,
      total_price:money
    })
    console.log(e.currentTarget.dataset.id);

  },
  //全选/取消全选
  checkAll:function(e){
    var page = this;
    var check_ids = [];
    var money = 0;
    if(page.data.selectAllStatus==true){
      page.setData({
        selectAllStatus: false,
        check_ids: check_ids,
        total_price: money
      })
    }else{
      for(var i in page.data.cart){
        check_ids[i] = page.data.cart[i].cart_id;
        money += page.data.cart[i].price * page.data.cart[i].num;
      }
      page.setData({
        selectAllStatus: true,
        check_ids: check_ids,
        total_price: money
      })
    }
  },
  //刷新显示价格
  reflushSelect:function(){
    var page = this;
    var ids = page.data.check_ids;
    var money = 0;
    for(var i in page.data.cart){
      if (ids.indexOf(page.data.cart[i].cart_id)!=-1){
        money += page.data.cart[i].price * page.data.cart[i].num;
      }
    }
    page.setData({
      total_price:money
    })
  },
  payOrder:function(){
    var page = this;
    if(page.data.check_ids.length<=0){
      wx.showToast({
        title: '请选择商品',
        icon: 'none'
      })
      return false;
    }else{
      wx.navigateTo({
        url: '../pay-order/pay-order?cart_ids=' + page.data.check_ids.toString(),
      })
    }
  },
  //推荐商品
  getRecgoods:function(){
    var page =this;
    app.request({
      url: api.shop.rec_goods,
      method: 'POST',
      data: {
      },
      success: function (res) {
        if (res.errcode == 0) {
          page.setData({
            rec_goods: res.data
          })
        }
      },
      fail: function (res) {
        wx.showModal({
          title: '警告',
          content: res.errmsg,
          showCancel: false
        })
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  }
})